package xyz.yuzegod.megawalls.classes.normal.creeper;

import net.citizensnpcs.api.CitizensAPI;
import net.minecraft.server.v1_8_R3.Entity;
import net.minecraft.server.v1_8_R3.World;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.*;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.nms.CustomCreeper;
import xyz.yuzegod.megawalls.util.EntityTypes;
import xyz.yuzegod.megawalls.util.ItemBuilder;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Creeper extends Classes
{
    public static final Map<GamePlayer, Integer> skill3Cooldown;
    
    public Creeper() {
        super("Creeper", "爬行者", ChatColor.GREEN, Material.TNT, (byte)0, ClassesType.NORMAL, new ClassesInfo.Orientation[] { ClassesInfo.Orientation.HURT, ClassesInfo.Orientation.CONTROL }, ClassesInfo.Difficulty.FOUR);
        this.setMainSkill(new MainSkill(this));
        this.setSecondSkill(new SecondSkill(this));
        this.setThirdSkill(new ThirdSkill(this));
        this.setCollectSkill(new FourthSkill(this));
        this.setDefaultSkin("eyJ0aW1lc3RhbXAiOjE1NDk0Mzc4NjM4MjQsInByb2ZpbGVJZCI6IjQ2N2NmOTRkY2UyYjQ1ZTY4YmRhNTJlNTUwMmU3M2U4IiwicHJvZmlsZU5hbWUiOiJMaWxpeWFfIiwic2lnbmF0dXJlUmVxdWlyZWQiOnRydWUsInRleHR1cmVzIjp7IlNLSU4iOnsidXJsIjoiaHR0cDovL3RleHR1cmVzLm1pbmVjcmFmdC5uZXQvdGV4dHVyZS80ZWI1OTFiYjI4MjdhYjEyMmVjYjQ1Y2QyM2JlMTc3Y2NlYTRlNjhhNDBkZGM2MjRjMjlhMzkyMTNhODUyYTNlIn19fQ==", "tASRja/DyrBoogF6V88aJYrZYaENBAJ3T37oqLqWnLxZucc7G8R/Eu7N7reoTAaZDElAjkVIHrxP8qR95EIO0cY/Z415l6jMzHsMcbTrWQ1Pw9QMFbstLpp+Xw38v7BLN+Xo00rh4nvPtNIv0Ozdb7V+9XXTntz9HRIbYIInqiSUeCKo6r8JmrhOjSCVhc1lHhNE/KDMT5sqvYREshvnE/5dX/+9YeVI14ufgtJ/IEuhk6fUdyDIgTg7fgU3TIpxRo4kT0Qd37ONhszIMSxMvbLohrZck6SYff8XuVPBSLBPaDHv9cwc1DwTEJ0+emEbVbIC3e8sLDfD1+byE+ZptXYjOiAfQypIzxSrECv9YuA8YkYI+yi+yButmaILbTZ3S4EazBGyeWkT/Gdb3tXMJV+uZnN5VxWKfJgULvh5bIjgXvPLYPoJKBoW1HljN2MvpnXaAMFBNU4+dWQ/Sr0/oc2MVbdWVbiow644aYuhTV+OZTkPFssALmt41NQd9eUBDn2MjBRhkvQuR0uVg3xciqQ3E2TF3taTly/dLw90L7MV9omWZRotVNKI7zPQmxM0rS+RB/l/q/23FlAAf5eUAHEO3mRRUTUEkfS3EnYVuGlfpTI+u4E8fJ6udPRorajHfARq+apXC5be6cHm7bH/fZxDFv0GluAFik/RFMPgOZA=");
        this.setEquipmentPackage(new EquipmentPackage(this) {
            @Override
            public List<ItemStack> getEquipments(final int level) {
                final List<ItemStack> items = new ArrayList<ItemStack>();
                items.add(new ItemBuilder(Material.COMPASS).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 指南针").build());
                items.add(new ItemBuilder(Material.DIAMOND_PICKAXE).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 镐").setUnbreakable(true).addEnchantment(Enchantment.DIG_SPEED, 3).addEnchantment(Enchantment.DURABILITY, 3).build());
                items.add(new ItemBuilder(Material.ENDER_CHEST).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 末影箱").build());
                switch (level) {
                    case 1: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_EXPLOSIONS, 2).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        break;
                    }
                    case 2: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_EXPLOSIONS, 3).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 2).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)2).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 3: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_EXPLOSIONS, 4).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 2).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 4: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_EXPLOSIONS, 5).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 5: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.DIAMOND_LEGGINGS).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_EXPLOSIONS, 6).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1).build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Creeper.this.nameColor + Creeper.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
    }
                }
                if (xyz.yuzegod.megawalls.util.ItemUtils.containsBow(items)) {
                    return items;
                }else {
                    items.add(new ItemBuilder(Material.BOW).setDisplayName(nameColor+getDisplayName()+" 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY,10).build());
                    return items;
                }
            }
            
            @Override
            public List<String> getInfo(final int level) {
                final List<String> lore = new ArrayList<String>();
                switch (level) {
                    case 1: {
                        lore.add(" §8▪ §7铁 剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 爆炸保护 II");
                        lore.add(" §8▪ §7治疗药水 8❤");
                        lore.add(" §8▪ §7牛排");
                        break;
                    }
                    case 2: {
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 爆炸保护 II ➜ §aIII");
                        lore.add(" §a+ §7速度药水 II");
                        lore.add("    §8▪ 0:15");
                        lore.add(" §8▪ §7牛排 §8x1 ➜ §ax2");
                        break;
                    }
                    case 3: {
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 爆炸保护 III ➜ §aIV");
                        lore.add(" §8▪ §7速度药水 §8x1 ➜ §ax2");
                        lore.add("    §8▪ 0:15");
                        break;
                    }
                    case 4: {
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 爆炸保护 IV ➜ §aV");
                        lore.add("    §a+ 保护 I");
                        lore.add(" §8▪ §7治疗药水 8❤ §8x1 ➜ §ax2");
                        lore.add(" §8▪ §7牛排 §8x2 ➜ §ax3");
                        break;
                    }
                    case 5: {
                        lore.add(" §8▪ §7铁 §8➜ §a钻石 §7护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 爆炸保护 V ➜ §aVI");
                        lore.add("    §8▪ 保护 I");
                        break;
                    }
                }
                return lore;
            }
        });
    }
    
    @Override
    public List<String> getInfo() {
        final List<String> lore = new ArrayList<String>();
        lore.add("§7你想在连击时加入一些小爆炸.");
        lore.add("§7你想掌控你的生命.");
        lore.add("§7你喜欢给那些毫无防备的矿工们惊喜(惊吓).");
        return lore;
    }
    
    @Override
    public int unlockCost() {
        return 25000;
    }
    
    @Override
    public int energyMelee() {
        return 20;
    }
    
    @Override
    public int energyBow() {
        return 20;
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        return this.mainSkill.getSkillTip(gamePlayer) + " " + this.thirdSkill.getSkillTip(gamePlayer);
    }
    
    @Override
    public void run() {
        GamePlayer.getOnlinePlayers().stream().filter(gamePlayer -> !gamePlayer.isSpectator() && ClassesManager.getSelected(gamePlayer).equals(this)).forEach(gamePlayer -> {
            if (Creeper.skill3Cooldown.getOrDefault(gamePlayer, 0) > 0) {
                Creeper.skill3Cooldown.put(gamePlayer, Creeper.skill3Cooldown.get(gamePlayer) - 1);
            }
            gamePlayer.sendActionBar(this.getSkillTip(gamePlayer));
        });
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerDeath(final PlayerDeathEvent e) {
        if (MegaWalls.getInstance().getGame().isWaiting()) {
            return;
        }
        final GamePlayer player = GamePlayer.get(e.getEntity().getUniqueId());
        final Classes classes = ClassesManager.getSelected(player);
        if (classes.equals(this)) {
            final SecondSkill secondSkill = (SecondSkill)this.getSecondSkill();
            final int level = secondSkill.getPlayerLevel(player);
            if (MegaWalls.getRandom().nextInt(100) <= secondSkill.getAttribute(level) * 100.0) {
                final CustomCreeper creeper = new CustomCreeper((World)((CraftWorld)Bukkit.getWorld("world")).getHandle());
                creeper.setGamePlayer(player);
                creeper.getBukkitEntity().setMetadata(MegaWalls.getMetadataValue(), (MetadataValue)new FixedMetadataValue((Plugin)MegaWalls.getInstance(), (Object)player.getGameTeam()));
                EntityTypes.spawnEntity((Entity)creeper, player.getPlayer().getLocation());
            }
        }
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onBlockPlace(final BlockPlaceEvent e) {
        if (e.isCancelled()) {
            return;
        }
        final GamePlayer gamePlayer = GamePlayer.get(e.getPlayer().getUniqueId());
        final Classes classes = ClassesManager.getSelected(gamePlayer);
        if (classes.equals(this) && e.getBlock().getType() == Material.TNT && !e.getPlayer().isSneaking()) {
            e.setCancelled(true);
            final Player player = e.getPlayer();
            final TNTPrimed tntPrimed = (TNTPrimed)player.getWorld().spawn(e.getBlock().getLocation().add(0.0, 1.0, 0.0), (Class)TNTPrimed.class);
            tntPrimed.setMetadata("CREEPER", (MetadataValue)new FixedMetadataValue((Plugin)MegaWalls.getInstance(), (Object)gamePlayer.getGameTeam()));
            tntPrimed.setFuseTicks(20);
            final ItemStack item = player.getItemInHand();
            if (item.getAmount() > 1) {
                item.setAmount(item.getAmount() - 1);
            }
            else {
                player.setItemInHand((ItemStack)null);
            }
            Bukkit.getScheduler().runTaskLater((Plugin)MegaWalls.getInstance(), (Runnable)new Runnable() {
                @Override
                public void run() {
                    for (final Player player1 : Creeper.this.getNearbyPlayers(player.getLocation(), player, 5)) {
                        player1.damage(((SecondSkill)Creeper.this.secondSkill).getDamage(gamePlayer.getPlayerStats().getKitStats(classes).getSkill2Level()), (org.bukkit.entity.Entity)player);
                    }
                }
            }, 20L);
        }
    }
    
    private List<Player> getNearbyPlayers(final Location location, final Player player, final int radius) {
        final List<Player> players = new ArrayList<Player>();
        final GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
        for (final Player other : PlayerUtils.getNearbyPlayers(location, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gameOther.isSpectator() && !gamePlayer.getGameTeam().isInTeam(gameOther)) {
                if (other.getLocation().distance(location) > radius) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityDamage(final EntityDamageEvent e) {
        if (CitizensAPI.getNPCRegistry().isNPC(e.getEntity())) {
            return;
        }
        if (e.isCancelled() || !(e.getEntity() instanceof Player)) {
            return;
        }
        final GamePlayer gamePlayer = GamePlayer.get(e.getEntity().getUniqueId());
        final Classes classes = ClassesManager.getSelected(gamePlayer);
        if (classes.equals(this)) {
            final Player player = (Player)e.getEntity();
            if (player.getHealth() - e.getFinalDamage() < 20.0) {
                this.thirdSkill.use(gamePlayer, gamePlayer.getPlayerStats().getKitStats(ClassesManager.getSelected(gamePlayer)));
            }
        }
    }
    
    static {
        skill3Cooldown = new HashMap<GamePlayer, Integer>();
    }
}
